home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / intl / datamgmt / example-1.1 / IntlWindow.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  5.5 KB  |  197 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18. import java.awt.event.*;
  19. import java.util.Locale;
  20.  
  21. import java.applet.Applet;
  22. import java.net.URL;
  23. import java.awt.image.ImageProducer;
  24. import java.awt.Toolkit;
  25.  
  26. public class IntlWindow extends Frame implements WindowListener {
  27.     Applet parentApplet = null;
  28.  
  29.     Panel cards;
  30.  
  31.     private Locale currentLocale;
  32.     private Locale[] supportedLocales = {
  33.     Locale.US,
  34.     Locale.FRANCE,
  35.     Locale.UK
  36.     };
  37.  
  38.     private Image[] flags;
  39.     private FlagCanvas[] flagCanvases;
  40.     private int selectedFlag = -1;
  41.  
  42.     private Image[] soundImages;
  43.     private String[] soundNames = { "book", "car", "dog", "apple" };
  44.  
  45.     private Font defaultFont;
  46.     private Font boldFont;
  47.  
  48.     public IntlWindow(Applet parentApplet) {
  49.  
  50.     currentLocale = getLocale();
  51.  
  52.     for (int i = 0; i < supportedLocales.length; i++) {
  53.         if (currentLocale == supportedLocales[i])
  54.         selectedFlag = i;
  55.     }
  56.  
  57.     if (selectedFlag == -1) {
  58.         currentLocale = supportedLocales[0];
  59.         selectedFlag = 0;
  60.     }
  61.  
  62.     this.parentApplet = parentApplet;
  63.     createGUIElements();
  64.     addWindowListener(this);
  65.     }
  66.  
  67.  
  68.     public static void main(String[] args) {
  69.         IntlWindow window = new IntlWindow(null);
  70.  
  71.         window.pack();
  72.         window.show();
  73.     }
  74.  
  75.     public void selectFlag(FlagCanvas f) {
  76.     for (int i = 0; i < flagCanvases.length; i++) {
  77.         if (f == flagCanvases[i]) {
  78.         selectedFlag = i;
  79.         flagCanvases[i].setSelected(true);
  80.         } else
  81.             flagCanvases[i].setSelected(false);
  82.     }
  83.     currentLocale = supportedLocales[selectedFlag];
  84.     ((CardLayout)cards.getLayout()).show(cards, currentLocale.getDisplayName());
  85.     doLayout();
  86.     }
  87.  
  88.     public void windowClosed(WindowEvent event) {
  89.     }
  90.  
  91.     public void windowDeiconified(WindowEvent event) {
  92.     }
  93.  
  94.     public void windowIconified(WindowEvent event) {
  95.     }
  96.  
  97.     public void windowActivated(WindowEvent event) {
  98.     }
  99.  
  100.     public void windowDeactivated(WindowEvent event) {
  101.     }
  102.  
  103.     public void windowOpened(WindowEvent event) {
  104.     }
  105.  
  106.     public void windowClosing(WindowEvent event) {
  107.         if (parentApplet != null) {
  108.             dispose();
  109.         } else {
  110.             System.exit(0);
  111.         }
  112.     }
  113.  
  114.     private void createGUIElements() {
  115.         defaultFont = new Font("Helvetica", Font.PLAIN, 14);
  116.         boldFont = new Font(defaultFont.getName(), Font.BOLD,
  117.                          defaultFont.getSize());
  118.         setFont(defaultFont);
  119.  
  120.     // load the images that are shared between all of the linguaPanels
  121.     // first the flags
  122.     MediaTracker tracker = new MediaTracker(this);
  123.     flags = new Image[supportedLocales.length];
  124.     flagCanvases = new FlagCanvas[supportedLocales.length];
  125.     for (int i = 0; i < supportedLocales.length; i++) {
  126.         String s = "flag_" + supportedLocales[i].toString() + ".gif";
  127.         try {
  128.             URL url = getClass().getResource(s);
  129.         flags[i] = Toolkit.getDefaultToolkit().getImage(url);
  130.         } catch (Exception e) {
  131.         System.err.println("Couldn't load flag image: " + s);
  132.         e.printStackTrace();
  133.         }
  134.         
  135.         tracker.addImage(flags[i], i);
  136.     }
  137.  
  138.     // then the sound images
  139.     soundImages = new Image[soundNames.length];
  140.     for (int i = 0; i < soundNames.length; i++) {
  141.         String s = soundNames[i] + ".gif";
  142.         try {
  143.             URL url = getClass().getResource(s);
  144.         soundImages[i] = Toolkit.getDefaultToolkit().getImage(url);
  145.         } catch (Exception e) {
  146.         System.err.println("Couldn't load sound image: " + s);
  147.         e.printStackTrace();
  148.         }
  149.         tracker.addImage(soundImages[i], i + supportedLocales.length);
  150.     }
  151.  
  152.     // wait for all of the images to be loaded
  153.         try {
  154.             tracker.waitForAll();
  155.         } catch (InterruptedException e) {
  156.         System.err.println("exception from tracker");
  157.         e.printStackTrace();
  158.     }
  159.  
  160.  
  161.     // now add the flag images to this window
  162.     Panel p = new Panel();
  163.     p.setLayout(new FlowLayout());
  164.     for (int i = 0; i < flags.length; i++) {
  165.        flagCanvases[i] = new FlagCanvas(flags[i],
  166.             this,
  167.             flags[i].getWidth(this),
  168.             flags[i].getHeight(this));
  169.        p.add(flagCanvases[i]);
  170.     }
  171.     flagCanvases[selectedFlag].setSelected(true);
  172.     add("North", p);
  173.  
  174.     // create a card panel
  175.     cards = new Panel();
  176.     cards.setLayout(new CardLayout());
  177.  
  178.     // create the LinguaPanels that go into the card layout
  179.     for (int i = 0; i < supportedLocales.length; i ++) {
  180.         LinguaPanel linguaPanel = null;
  181.         try {
  182.         linguaPanel = new LinguaPanel(supportedLocales[i], parentApplet,
  183.                                   defaultFont, boldFont, soundImages, soundNames);
  184.         } catch (java.util.MissingResourceException e) {
  185.         System.err.println("Couldn't create a LinguaPanel for " + supportedLocales[i]);
  186.         System.err.println(e);
  187.         }
  188.  
  189.         // add it to the card layout
  190.         cards.add(supportedLocales[i].getDisplayName(), linguaPanel);
  191.     }
  192.  
  193.     // add the card layout to this window
  194.     add("Center", cards);
  195.     }
  196. }
  197.